package org.s3.project.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.checkerframework.checker.units.qual.A;
import org.s3.project.common.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.s3.project.common.R;
import org.s3.project.entity.Comment;

import org.s3.project.entity.addComment;
import org.s3.project.service.CommentService;
import org.s3.project.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:28
 */
@CrossOrigin(origins = "*")
@RestController
public class CommentController {
    @Autowired
    CommentService commentService;
    @Autowired
    OrdersService ordersService;
    @PostMapping("insertComments")
    Boolean insertComments(@RequestBody List<Comment> comment){

        return  commentService.insertComments(comment);
    }
    //根据id查询评论
    @GetMapping("/getCommentById/{id}")
    public List<Comment> getCommentById(@PathVariable("id") Integer id){
        return commentService.getCommentById(id);
    }
    /**
     *
     * 获取评论表总数
     * @return 评论表总数
     */
    @GetMapping("getCommentCount")
    public Map<String, Object> getCommentCount() {
        long commentCount = commentService.count();
        Map<String, Object> result = new HashMap<>();
        result.put("commentCount", commentCount);
        return result;
    }
    //根据用户id查询评论
    @GetMapping("/getCommentByUserId/{User_id}")
    public List<Comment> getCommentByUserId(@PathVariable("User_id") Integer User_id){
        return commentService.getCommentByUserId(User_id);
    }
    //删除评论
    @GetMapping("/deleteComment/{id}")
    public Integer deleteComment(@PathVariable("id") Integer id){
        return commentService.deleteCommentById(id);
    }


    /**
     * 分页查询评论信息
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @return 评论信息列表
     */
    @GetMapping("showComment/{page}")
    public R selectUCommentPage(@PathVariable("page")int page, Integer pageSize,
                                @RequestParam(required = false) String Mname,
                                @RequestParam(required = false) String Uname,
                                @RequestParam(required = false) String created_time,
                                @RequestParam(required = false) String create_timeOne){
        PageHelper.startPage(page,pageSize);//设置分页
        List<Comment> commentList= this.commentService.selectCommentLike(Mname,Uname,created_time,create_timeOne);
        PageInfo<Comment> commentPageList = new PageInfo<>(commentList);
        return new R().add("commentPageList",commentPageList);
    }

    /**
     *
     * 删除单个评论
     * @param id 评论id
     * @return 删除结果
     */
    @GetMapping("deleteCommentId")
    public int deleteComment(int id){
        return commentService.deleteComment(id);
    }

    /**
     * 批量删除评论
     * @param ids 评论id数组
     */
    @GetMapping("deleteCommentIds")
    public int deleteCommentIds(int[] ids) {
        return commentService.deleteCommentIds(ids);
    }
}
